package com.funny.stock.user.controller;

import com.funny.stock.base.util.ResultUtil;
import com.funny.stock.base.vo.Result;
import com.funny.stock.user.dao.pojo.UserInfoDO;
import com.funny.stock.user.service.UserInfoService;
import com.funny.stock.user.util.UserSecurityUtil;
import com.funny.stock.user.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author pengl
 * @date 2020/11/24
 **/
@RestController
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;


    @GetMapping("/info")
    public Result<UserInfoDO> detail(){
        Long userId = UserSecurityUtil.getUserId();
        UserInfoDO userInfoDO = userInfoService.findById(userId).orElse(null);
        return ResultUtil.success(userInfoDO);
    }


    @PostMapping("/update")
    public Result<UserInfoDO> register(@RequestBody UserInfoVO userInfoVO){
        Long userId = UserSecurityUtil.getUserId();
        if(StringUtils.isEmpty(userInfoVO.getName())){
            return ResultUtil.error("昵称不能为空");
        }

        if(StringUtils.isEmpty(userInfoVO.getEmail())){
            return ResultUtil.error("email不能为空");
        }

        UserInfoDO userInfoDO = userInfoService.updateUser(userId,userInfoVO);

        return ResultUtil.success(userInfoDO);
    }

}
